package com.neusoft.bizcore.notice.notification.service.impl;

import java.util.List;

import javax.persistence.EntityNotFoundException;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.notice.notification.converter.NoticeRecordConverter;
import com.neusoft.bizcore.notice.notification.dto.NoticeRecordDTO;
import com.neusoft.bizcore.notice.notification.model.NoticeRecord;
import com.neusoft.bizcore.notice.notification.repository.NoticeRecordRepository;
import com.neusoft.bizcore.notice.notification.service.NoticeRecordService;
import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.bizcore.web.support.Searchable;

@Service
@Transactional
public class NoticeRecordServiceImpl implements NoticeRecordService {

    @Autowired
    private NoticeRecordRepository noticeRecordRepository;

    @Autowired
    private NoticeRecordConverter noticeRecordConverter;

    @Override
    public List<NoticeRecordDTO> findAll(final Searchable searchable) {
        final List<NoticeRecord> models = this.noticeRecordRepository.index(searchable);
        return this.noticeRecordConverter.toListDTO(models);
    }

    @Override
    public Page<NoticeRecordDTO> search(final Pageable pageable, final Searchable searchable) {
        final Page<NoticeRecord> pages = this.noticeRecordRepository.search(pageable, searchable);
        return this.noticeRecordConverter.toPageDTO(pages);
    }

    @Override
    public NoticeRecordDTO create(final NoticeRecordDTO dto) {
        final NoticeRecord model = this.noticeRecordConverter.toModel(dto);
        this.noticeRecordRepository.save(model);
        final NoticeRecordDTO noticeDTO = this.noticeRecordConverter.toDTO(model);
        return noticeDTO;
    }

    @Override
    public NoticeRecordDTO update(final Long id, final NoticeRecordDTO dto) {
        NoticeRecord model;
        try {
            model = this.noticeRecordRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }
        dto.setId(model.getId());
        model = this.noticeRecordConverter.toModel(dto);
        this.noticeRecordRepository.save(model);
        return this.noticeRecordConverter.toDTO(model);
    }

    @Override
    public NoticeRecordDTO details(final Long id) {
        final NoticeRecord model;
        try {
            model = this.noticeRecordRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        return this.noticeRecordConverter.toDTO(model);
    }

    @Override
    public void delete(final Long id) {
        this.noticeRecordRepository.deleteById(id);
    }

}
